<template>
    <view class="chartNum">
        <view class="box-item">
            <view
                :class="{'number-item': !isNaN(item), 'mark-item': isNaN(item) }"
                v-for="(item,index) in orderNum"
                :key="index"
            >
                <span v-if="!isNaN(item)">
                    <ul ref="numberItem">
                        <li v-for="(i,t) in 10" :key="t">{{t}}</li>
                    </ul>
                </span>
                <span class="comma" v-else>{{item}}</span>
            </view>
        </view>
    </view>
</template>
<script>
export default {
  props: ["num"],
  data() {
    return {
      orderNum: ["0", "0", ".", "0", "0"] // 默认总数
    };
  },
  mounted() {},
  watch: {
    num(v) {
		console.log(v)
      this.toOrderNum(v);
    }
  },
  methods: {
    // 设置文字滚动
    setNumberTransform() {
      const numberItems = this.$refs.numberItem; // 拿到数字的ref，计算元素数量
	  console.log(numberItems)
      const numberArr = this.orderNum.filter(item => !isNaN(item));
      // 结合CSS 对数字字符进行滚动,显示订单数量
      for (let index = 0; index < numberItems.length; index++) {
        const elem = numberItems[index];
        elem.style.transform = `translate(-50%, -${numberArr[index] * 10}%)`;
      }
    },
    // 处理总订单数字
    toOrderNum(num) {
      num = num.toString();
	  console.log(num)
      // 把订单数变成字符串
      if (num.length < 4) {
        num = "0" + num; // 如未满四位数，添加"0"补位
        this.toOrderNum(num); // 递归添加"0"补位
      } else if (num.length === 4) {
        // 订单数中加入逗号
        num = num.slice(0, 2) + "." + num.slice(2, 4);
        this.orderNum = num.split(""); // 将其便变成数据，渲染至滚动数组
      } else {
        // this.$message.warning("订单总量数字过大，显示异常，请联系客服");
      }
      this.setNumberTransform();
    }
  }
};
</script>
<style scoped lang='scss'>
/*订单总量滚动数字设置*/
.box-item {
  display: flex;
  position: relative;
  height: 100rpx;
  //   font-size: 54rpx;
  line-height: 41rpx;
  text-align: center;
  list-style: none;
  color: #ff455c;
  //   writing-mode: vertical-lr;
  //   text-orientation: upright;
  /*文字禁止编辑*/
  -moz-user-select: none; /*火狐*/
  -webkit-user-select: none; /*webkit浏览器*/
  -ms-user-select: none; /*IE10*/
  -khtml-user-select: none; /*早期浏览器*/
  user-select: none;
  /* overflow: hidden; */
}
/* 默认逗号设置 */
.mark-item {
  width: 10rpx;
  height: 100rpx;
  margin:0 10rpx;
  line-height: 100rpx;
    font-size: 120rpx;
  position: relative;
  & > span {
    position: absolute;
    width: 100%;
    bottom: 0;
    bottom: 12rpx;
    left: -10rpx;
    // writing-mode: vertical-rl;
    // text-orientation: upright;
  }
}
/*滚动数字设置*/
.number-item {
  width: 61rpx;
  height: 100rpx;
  list-style: none;
  margin: 0 5rpx;
  border-radius: 4rpx;
  & > span {
    position: relative;
    display: inline-block;
    margin-right: 10rpx;
    width: 100%;
    height: 100%;
    // writing-mode: vertical-rl;
    // text-orientation: upright;
    overflow: hidden;
    & > ul {
      width: 100%;
      font-style: normal;
      position: absolute;
      /* left: 50%; */
      transform: translate(-50%, 0);
      transition: transform 1s ease-in-out;
      letter-spacing: 10rpx;
      li {
		list-style: none;
        height: 100rpx;
        text-align: center;
        line-height: 100rpx;
        font-weight: bold;
      }
    }
  }
}
.number-item:last-child {
  margin-right: 0;
}
</style>